var chat = {
	//连接建立时发送登录信息
	onopen : function(){
		name = localStorage.getItem("username");
	    // 登录
	    var login_data = '{"type":"loginchatroom","username":"'+name.replace(/"/g, '\\"')+'","room_id":"'+into_room_id+'"}';
	    ws.send(login_data);
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
		console.log(JSON.stringify(data));
	    switch(data['type']){
	        // 登录 更新用户列表
	        case 'loginchatroom':
	            //{"type":"login","client_id":xxx,"client_name":"xxx","client_list":"[...]","time":"xxx"}
	            localStorage.setItem("client_id",data['client_id']);
	            chat.say(data['client_id'], data['username'],  data['username']+' 加入了聊天室', data['time'],'info');
	            if(data['client_list'])
	            {
	                client_list = data['client_list'];
	            }
	            else
	            {
	                client_list[data['client_id']] = data['username']; 
	            }
	            chat.flush_client_list();
	            break;
	        // 发言
	        case 'say':
	            //{"type":"say","from_client_id":xxx,"to_client_id":"all/client_id","content":"xxx","time":"xxx"}
	            console.log(data['client_id']);
	            console.log(localStorage.getItem("client_id"));
	            if(localStorage.getItem("client_id")==data['client_id']) whereSay = "me";
	            chat.say(data['from_client_id'], data['from_client_name'], data['content'], data['time'],whereSay);
	            break;
	        // 用户退出 更新用户列表
	        case 'logout':
	            //{"type":"logout","client_id":xxx,"time":"xxx"}
	            chat.say(data['from_client_id'], data['from_client_name'], data['from_client_name']+' 退出了', data['time'],'info');
	            delete client_list[data['from_client_id']];
	            chat.flush_client_list();
	    }
	},
	// 提交对话
	onSubmit : function() {
	  var input = document.getElementById("sayContent");
	  if(input.value=="") return false;
	  var to_client_id = 'all';//$("#client_list option:selected").attr("value");
	  var to_client_name = '所有人';//$("#client_list option:selected").text();
	  ws.send('{"type":"say","to_client_id":"'+to_client_id+'","to_client_name":"'+to_client_name+'","content":"'+input.value.replace(/"/g, '\\"').replace(/\n/g,'\\n').replace(/\r/g, '\\r')+'"}');
	  input.value = "";
	  input.focus();
	},
	// 刷新用户列表框(人数)
	flush_client_list : function(){
		var online_number = document.querySelector("#online-number");
		var client_count = 0;
		for(var p in client_list){
	        client_count ++;
	    }
		online_number.innerHTML = client_count;
	},
	// 发言
	say : function(from_client_id, from_client_name, content, time,saytype){
		var chatBody = document.querySelector("#game-chat-body");
		var sayData = {
			name : from_client_name,
			content : content,
			time : time,
			client_id : localStorage.getItem("client_id"),
		}
		switch(saytype){
			case 'info':
				infoSay(chatBody,content);
				break;
			case 'she':
				sheSay(chatBody,sayData)
				break;
			case 'me':
				meSay(chatBody,sayData)
				break;
		}
		hb_settime(document.querySelector("#hb_1"));
		//MUI动态加载数据后，scrollToBottom无效的解决方案
		////页面加载后，初始化滚动条
		var scroll = mui('#app-chat-scroll').scroll({indicators: false});
		//重新计算布局值，最大滚动的高度等等
		scroll.reLayout();
		//滚动到底部
		scroll.scrollToBottom(100);
	},
};
mui.init();
mui.plusReady(function(){
	var curView = plus.webview.currentWebview();
	curView.setStyle({softinputMode: "adjustResize"});
	into_room_id = localStorage.getItem('room_id');
	name = localStorage.getItem('username');
	if(name == "default_name"){
		mui.back();
	}
	room_connect(chat.onopen,chat.onmessage);
	mui("body").on("tap","a#sayButton",function(e){
		chat.onSubmit();
	},false);
	mui("body").on("tap","#sayMore",function(e){
		document.querySelector("#sendMore").style.display = "block";
	});
	mui("body").on("focus","input#sayContent",function(e){
		//页面加载后，初始化滚动条
		var scroll = mui('#app-chat-scroll').scroll({indicators: false});
		//重新计算布局值，最大滚动的高度等等
		scroll.reLayout();
		//滚动到底部
		scroll.scrollToBottom(100);
	});
	mui("#betCoinNumberKeyBoard").on("tap","a",function(e){
		var curNumber = this.getAttribute("data-id");
		var betCoinInput = document.querySelector("#betCoin");
		switch(curNumber){
			case "clear":
				//重置
				betCoinInput.value = '';
				break;
			case "cancel":
				//取消投注
				
				document.querySelector("#sendMore").style.display = "none";
				break;
			default:
				betCoinInput.value = betCoinInput.value + '' + curNumber;
				break;
		}
		
		
	});
	mui("body").on("tap","#okeyBetSubmit",function(e){
		//投注
		var betCoinInput = document.querySelector("#betCoin");
		if(betCoinInput.value=='' || parseInt(betCoinInput.value)==0){
			mui.toast("投注金额不能为0");
			return false;
		}
		
		console.log('begin bet...');
	});
	//抢做庄家
	mui("body").on("tap","#LandlordSnatching",function(e){
		console.log("抢庄家");
	});
	mui("body").on("tap",".game-chat-sendhb",function(e){
		console.log('red');
		//先检测本次是否有下注没有下注即返回
		var isBet = localStorage.getItem("isBet");
		if(isBet!=="true"){
			mui.toast("哦哦，没有下注，怎么抢红包呢？");
			//return false;
		}
		document.querySelector("#showHb").style.display = 'block';
	});
	mui("body").on("tap","#close-hb",function(e){
		document.querySelector("#showHb").style.display = 'none';
		localStorage.setItem("isBet","false");
	});
});